package TextRank;

import Ast.AstUtil;
import Ast.MethodVisitor;
import Count.CountApiStructure;
import dataStructure.Function;
import org.eclipse.jdt.core.dom.CompilationUnit;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class OperateStructure {
    public void OperateStructure(String path) throws IOException {
        //使用ast从源码中获取函数体
        CompilationUnit  comp = AstUtil.getCompilationUnit(path);
        List<Function> list = new ArrayList<>();
        MethodVisitor mv = new MethodVisitor(list);
        comp.accept(mv);
//        String strtest = list.get(2).getFunbady();

        //CountApi用来统计api的个数
        CountApiStructure ca = new CountApiStructure();
        TextRank tr = new TextRank();   //TextRank用来实现关键句摘要

        //获取输出路径
        String outpath = GetOutPath(path);
        try {
            File file = new File(outpath);
            if(!file.exists()){
                file.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fileWriter);

            //获取输出结果并写入输出文件
            for (Function i : list) {
                //统计函数中使用api的个数
                int temp = ca.CountApiStructure(i.getFunbady());
                if (temp == 0 ){                   //摘要为空
//                    System.out.println(i.getFunname()+"{ }");
                    bw.write(i.getFunname()+"{ }"+"\n");
                }else{
                    List<String> resulttest = new ArrayList<>();
                    resulttest=tr.TextRank(i.getFunbady(),temp);
//                    System.out.println(i.getFunname()+"{");
                    bw.write(i.getFunname()+"{"+"\n");
                    for (String str : resulttest) {
//                        System.out.println(str);
                        bw.write(str+"\n");
                    }
//                    System.out.println("}");
                    bw.write("}"+"\n");
                }
            }
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //获取输出文件路径
    public String GetOutPath(String inputpath){
        String outpath = "";
        outpath = inputpath.replace("resource","resultStructure");
        return outpath;
    }
}

